package com.tp.controller.admin;

import com.jfinal.aop.Inject;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.Ret;
import com.tp.base.BaseController;
import com.tp.model.sys.SysLog;
import com.tp.model.sys.SysUser;
import com.tp.service.UserService;
import com.tp.utils.EncryptionUtil;
import com.tp.utils.JCaptchaRender;
import com.tp.utils.StrUtils;
import com.tp.utils.WebUtils;

/**
 * adminController
 */
@ControllerBind(controllerKey = "/")
public class AdminController extends BaseController {

	@Inject
	UserService service;

	private static final String loginPage = "/pages/admin/login.html";
	private static final String homePage = "/home";
	private static final String homePath = "/pages/admin/home/";

	/**
	 * 默认页面
	 */
	public void index() {
		if (getSessionUser() != null) {
			// 如果session存在，不再验证
			redirect(homePage);
		} else {
			render(loginPage);
		}
	}

	/**
	 * 主页
	 */
	public void home() {
		SysUser user = getSessionUser();
		if (StrUtils.empty(user)) {
			render(loginPage);
		} else {
			render(homePath + "index.html");
		}
	}
	
	/**
	 * 控制台页面
	 */
	public void welcome() {
		render(homePath + "welcome.html");
	}

	/**
	 * 登录
	 */
	public void login() {
		// 获取登录参数
		String account = this.getPara("account");
		String password = this.getPara("password");
		String checkCode = this.getPara("vercode");
		if (StrUtils.isEmpty(checkCode) || !JCaptchaRender.validate(this, checkCode)) {
			renderFail("验证码错误");
			return;
		}
		if (StrUtils.isEmpty(account)) {
			renderFail("帐号不能为空");
			return;
		} else if (StrUtils.isEmpty(password)) {
			renderFail("密码不能为空");
			return;
		}
		// md5加密
		String encryptPassword = EncryptionUtil.encryptMD5ToString(password).toLowerCase();
		SysUser user = service.getUserByAccount(account);
		if (user == null || user.getId() <= 0) {
			renderFail("认证失败，请您重新输入。");
			return;
		}
		String md5Password = "";
		try {
			String userPassword = user.getPassword();
			String decryptPassword = WebUtils.passwordDecrypt(userPassword);
			// System.out.println(">> " + decryptPassword);
			md5Password = EncryptionUtil.encryptMD5ToString(decryptPassword).toLowerCase();
		} catch (Exception e) {
			log.error("认证异常", e);
			renderFail("认证失败，请您重新输入。");
			return;
		}
		if (!md5Password.equals(encryptPassword)) {
			renderFail("密码错误，请您重新输入。");
			return;
		}
		if (!(user.getUserType() == 1 || user.getUserType() == 2)) {
			renderFail("您没有登录权限，请您重新输入。");
			return;
		}
		setSessionUser(user);
		// 添加日志
		user.put("update_id", user.getUserId());
		user.put("update_time", getNow());
		service.saveLog(user, SysLog.SYSTEM_LOGIN);
		renderOk("登录成功");
	}

	/**
	 * 登出
	 */
	public void logout() {
		SysUser user = getSessionUser();
		if (user != null) {
			// 添加日志
			user.put("update_id", user.getUserId());
			user.put("update_time", getNow());
			service.saveLog(user, SysLog.SYSTEM_LOGOUT);
			// 删除session
			removeSessionUser();
		}
		renderJson(Ret.by("code", "1001").set("msg", "退出成功"));
	}

	/**
	 * 验证码
	 */
	public void imageCodes() {
		render(new JCaptchaRender());
	}

	/**
	 * 提示页面
	 */
	public void trans() {
		String redirectPath = getPara();
		if (StrUtils.isEmpty(redirectPath)) {
			redirectPath = "/pages/error/trans.html";
		} else if (redirectPath.equals("auth")) {
			redirectPath = "/pages/error/trans_no_auth.html";
		}
		render(redirectPath);
	}
}
